package com.ruoyi.app.controller

import cn.hutool.core.bean.BeanUtil
import com.aliyun.sts20150401.models.AssumeRoleResponseBody
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Ok
import com.ruoyi.app.common.Sts
import com.ruoyi.app.domain.dto.*
import com.ruoyi.app.domain.vo.AppVersionInfoVo
import com.ruoyi.app.service.LoginApiService
import com.ruoyi.back.domain.AppUser
import com.ruoyi.back.domain.AppVersionInfo
import com.ruoyi.back.service.AppUserService
import com.ruoyi.back.service.AppVersionInfoService
import com.ruoyi.back.service.UserRecordService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

@RestController
@Api(tags = ["登录相关接口"])
@RequestMapping("/api/login")
class LoginApiController {

    @Autowired
    private lateinit var loginApiService: LoginApiService

    @Autowired
    private lateinit var appVersionInfoService: AppVersionInfoService

    @Autowired
    private lateinit var sts: Sts

    @Autowired
    private lateinit var appUserService: AppUserService

    @Autowired
    private lateinit var userRecordService: UserRecordService

    @ApiOperation("三方登陆接口(qq 微信登陆)")
    @PostMapping("threeLogin")
    fun threeLogin(threeLoginDto: ThreeLoginDto): Data<String> = loginApiService.threeLogin(threeLoginDto)

    @ApiOperation("重置用户的使用次数")
    @PostMapping("resetAppUserUseCount")
    fun resetAppUserUseCount(): Data<Boolean> {

        appUserService.updateBatchById(appUserService.list(KtQueryWrapper(AppUser::class.java)
            .ne(AppUser::userType, 1)).map { appUser ->
            appUser.apply {
                changeFaceRemainCount = userRecordService.getUserChangeFaceNum(appUser.id!!).run {
                    takeIf { it > (appUser.totalChangeFaceCount ?: 0) }?.run { appUser.totalChangeFaceCount }
                        ?: this.toInt()
                }
            }
        })

        return Ok("更新成功", true)
    }

    @GetMapping("sts")
    @ApiOperation("获取stsToken")
    fun sts(): Data<AssumeRoleResponseBody.AssumeRoleResponseBodyCredentials> = Ok("获取token成功", sts.stsToken())

    @ApiOperation("一键登陆")
    @PostMapping("oneKeyLogin")
    fun oneKeyLogin(oneKeyLoginDto: OneKeyLoginDto): Data<String> = loginApiService.oneKeyLogin(oneKeyLoginDto)

    @ApiOperation("手机号登录(验证码登录)")
    @PostMapping("phoneLogin")
    fun phoneLogin(phoneLoginDto: PhoneLoginDto): Data<String> = loginApiService.phoneLogin(phoneLoginDto)

    @ApiOperation("发送短信验证码")
    @GetMapping("sendSms/{phone}")
    fun sendSms(@PathVariable phone: String): Data<String> = loginApiService.sendSms(phone)

    @ApiOperation("手机号注册")
    @PostMapping("register")
    fun register(registerDto: RegisterDto): Data<String> = loginApiService.register(registerDto)

    @ApiOperation("忘记密码")
    @PostMapping("forgetPassword")
    fun forgetPassword(registerDto: RegisterDto): Data<Boolean> = loginApiService.forgetPassword(registerDto)

    @ApiOperation("手机号密码登录")
    @PostMapping("passwordLogin")
    fun passwordLogin(phonePasswordLoginDto: PhonePasswordLoginDto): Data<String> = loginApiService.passwordLogin(phonePasswordLoginDto)

    @ApiOperation("获取用户协议")
    @GetMapping("getUserAgreement")
    fun getUserAgreement(): Data<String?> = loginApiService.getUserAgreement()

    @ApiOperation("获取隐私政策")
    @GetMapping("getPrivacyPolicy")
    fun getPrivacyPolicy(): Data<String?> = loginApiService.getPrivacyPolicy()

    @ApiOperation("获取版本号信息")
    @GetMapping("getLastVersionInfo")
    fun getLastVersionInfo(): Data<AppVersionInfoVo> = Ok("获取最新版本号信息成功", appVersionInfoService.getOne(
        KtQueryWrapper(AppVersionInfo::class.java)
            .orderByDesc(AppVersionInfo::createTime)
            .last("limit 1")
    )?.let { AppVersionInfoVo().apply { BeanUtil.copyProperties(it, this) } })

}